package com.chunxuan.langquan.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.GsonUtils;
import com.chunxuan.langquan.R;
import com.chunxuan.langquan.base.BaseResult2;
import com.chunxuan.langquan.base.Global;
import com.chunxuan.langquan.dao.bean.CarProductEntity;
import com.chunxuan.langquan.dao.bean.NewOrderInfo;
import com.chunxuan.langquan.dao.bean.OrderBottomBean;
import com.chunxuan.langquan.dao.bean.OrderInfo;
import com.chunxuan.langquan.dao.bean.ProductBean;
import com.chunxuan.langquan.dao.http.APIRetrofit;
import com.chunxuan.langquan.support.util.AlertUtil;
import com.chunxuan.langquan.support.util.GlideUtil;
import com.chunxuan.langquan.ui.activity.OrderListActivity;
import com.chunxuan.langquan.ui.activity.store.OrdersActivity;
import com.ruitu.arad.api.RxUtil;
import com.ruitu.arad.util.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

public class MyOrderListAdapter extends RecyclerView.Adapter {
//    private List<OrderInfo> orderInfoList;
    private Context mContext;
    private List<Object> data;
    private View headerView;
    private int ITEM_HEADER = 1,ITEM_CONTENT = 2,ITEM_FOOTER = 3;
    private ProductBean headerBean;
    public MyOrderListAdapter(Context context,List<Object> data){
        this.mContext=context;
        this.data=data;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if(viewType==ITEM_CONTENT){
            View v = LayoutInflater.from(mContext).inflate(R.layout.item_order_header, parent, false);
            TitleViewHolder titleViewHolder = new TitleViewHolder(v);
            return titleViewHolder;
        }else if(viewType==ITEM_FOOTER){
            View v2 = LayoutInflater.from(mContext).inflate(R.layout.item_order_bottom, parent, false);
            MyViewHolder myViewHolder = new MyViewHolder(v2);
            return myViewHolder;
        }
        return null;
    }
    @Override
    public int getItemViewType(int position) {
        if(data.get(position) instanceof ProductBean){
            return ITEM_CONTENT;
        }else if(data.get(position) instanceof OrderBottomBean){
            return ITEM_FOOTER;
        }
        return ITEM_CONTENT;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, final int position) {
        if(holder instanceof TitleViewHolder){
            ProductBean productBean= (ProductBean) data.get(position);
            headerBean=productBean;
            ((TitleViewHolder)holder).tvProductTitle.setText(productBean.getTitle());
            ((TitleViewHolder)holder).tvProductPrice.setText(productBean.getPrice());
            ((TitleViewHolder)holder).tvProductNum.setText("x"+productBean.getNumber());
            GlideUtil.loadImg(productBean.getImage(),((TitleViewHolder)holder).ivProductImage);

        }else if(holder instanceof MyViewHolder){
            OrderBottomBean orderBottomBean= (OrderBottomBean) data.get(position);
            ((MyViewHolder)holder).tvOrderNum.setText(orderBottomBean.getOut_trade_no());
            ((MyViewHolder)holder).tvTotalPrice.setText(orderBottomBean.getTotal_price());
            ((MyViewHolder)holder).tvTotalNum.setText(orderBottomBean.getTotal_num()+"");
            if(orderBottomBean.getHave_paid()==0){
                ((MyViewHolder)holder).tv1.setVisibility(View.VISIBLE);
                if(orderBottomBean.getStatus()==1){
                    ((MyViewHolder)holder).tv1.setText("去付款");
                    ((MyViewHolder)holder).tv1.setEnabled(true);
                    ((MyViewHolder)holder).tv2.setVisibility(View.VISIBLE);
                    ((MyViewHolder)holder).tv2.setText("取消订单");
                }else if(orderBottomBean.getStatus()==0){
                    ((MyViewHolder)holder).tv1.setText("已取消");
                    ((MyViewHolder)holder).tv1.setEnabled(false);
                    ((MyViewHolder)holder).tv2.setVisibility(View.INVISIBLE);
//                        ((MyViewHolder)holder).tv2.setText("取消订单");
                }
                ((MyViewHolder)holder).tv2.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        showDialog(orderBottomBean.getOrder_id());
                    }
                });
            }else{
                if(orderBottomBean.getRefund_status_text().contains("申请中")){
                    ((MyViewHolder)holder).tv1.setVisibility(View.VISIBLE);
                    ((MyViewHolder)holder).tv1.setText("退款中");
                    ((MyViewHolder)holder).tv1.setEnabled(false);
                    ((MyViewHolder)holder).tv2.setVisibility(View.GONE);
                }else{
                    ((MyViewHolder)holder).tv1.setVisibility(View.VISIBLE);
                    ((MyViewHolder)holder).tv1.setText("申请退款");
                    ((MyViewHolder)holder).tv1.setEnabled(true);
                    ((MyViewHolder)holder).tv2.setVisibility(View.GONE);
                }
            }
            ((MyViewHolder)holder).tv1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(((MyViewHolder)holder).tv1.getText().toString().contains("去付款")){
                        Intent intent = new Intent(mContext, OrdersActivity.class);
                        List<CarProductEntity> list = new ArrayList<>();


                            CarProductEntity productEntity = new CarProductEntity();
                            productEntity.setImage(headerBean.getImage());
                            productEntity.setSales_price(Float.parseFloat(headerBean.getPrice()));
                            productEntity.setNumber(1);
                            productEntity.setProduct_id(String.valueOf(headerBean.getId()));
                            list.add(productEntity);
                        intent.putExtra("data", GsonUtils.toJson(list));
                        mContext.startActivity(intent);
                    }else if(((MyViewHolder)holder).tv1.getText().toString().contains("申请退款")){
                        showDialog(orderBottomBean.getOrder_id(),
                                orderBottomBean.getTotal_price(),
                                orderBottomBean.getOrder_product_id());
                    }
                }
            });


        }
    }
    private AlertDialog alert1;// 弹窗
    private View alert_view1;
    private void showDialog(String order_id, String total_price, int order_product_id) {
        // 弹窗
        alert1 = new AlertDialog.Builder(mContext, R.style.Dialog_FS).create();
        alert1.show();
        alert1.setCancelable(false);
        alert_view1 = View.inflate(mContext, R.layout.layout_alert_video_pop_alert, null);
        TextView tv_title = alert_view1.findViewById(R.id.tv_title);
        tv_title.setText("确定要申请退款吗?");
        TextView tv_Confirm = alert_view1.findViewById(R.id.tv_2);
        tv_Confirm.setText("确定");
        alert_view1.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert1.dismiss();
            }
        });
        alert_view1.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //申请退款
                refund(order_id,total_price,order_product_id);
                alert1.dismiss();
            }
        });
        OrderListActivity orderListActivity= (OrderListActivity) mContext;
        AlertUtil.setAlertCenterStyle(alert1.getWindow(), alert_view1, orderListActivity);
    }

    private Disposable refund(String order_id, String total_price, int order_product_id) {
        return APIRetrofit.getDefault().refund(Global.HEADER,order_id,total_price,String.valueOf(order_product_id))
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 result2) throws Exception {
                        if(result2.isSuccess()){
                            ToastUtils.showShort(result2.getMsg());
                            notifyDataSetChanged();
                        } else{
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("申请退款失败");
                    }
                });
    }

    private AlertDialog alert;// 弹窗
    private View alert_view;
    public void showDialog(final String order_id) {// 弹窗
        alert = new AlertDialog.Builder(mContext, R.style.Dialog_FS).create();
        alert.show();
        alert.setCancelable(false);
        alert_view = View.inflate(mContext, R.layout.layout_alert_video_pop_alert, null);
        TextView tv_title = alert_view.findViewById(R.id.tv_title);
        tv_title.setText("确定要取消订单吗?");
        TextView tv_Confirm = alert_view.findViewById(R.id.tv_2);
        tv_Confirm.setText("确定");
        alert_view.findViewById(R.id.tv_1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert.dismiss();
            }
        });
        alert_view.findViewById(R.id.tv_2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancelOrder(order_id);
                alert.dismiss();
            }
        });
        OrderListActivity orderListActivity= (OrderListActivity) mContext;
        AlertUtil.setAlertCenterStyle(alert.getWindow(), alert_view, orderListActivity);
    }
    public void addData(List<Object> list){
        data.addAll(list);
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        int count = (data == null ? 0 : data.size());
        if(headerView != null){
            count++;
        }
        return count;
    }
    public View getHeaderView(){
        return headerView;
    }
    private Disposable cancelOrder(String order_id) {
        return APIRetrofit.getDefault().cancelOrder(Global.HEADER,order_id)
                .compose(RxUtil.<BaseResult2>subIoObMain())
                .subscribe(new Consumer<BaseResult2>() {
                    @Override
                    public void accept(BaseResult2 result2) throws Exception {
                        if(result2.isSuccess()){
                            ToastUtils.showShort(result2.getMsg());
                            notifyDataSetChanged();
                        } else{
                            ToastUtils.showShort(result2.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
//                        hideProgress();
                        throwable.printStackTrace();
                        ToastUtils.showShort("取消订单失败");
                    }
                });
    }
    public static class TitleViewHolder extends RecyclerView.ViewHolder {
        ImageView ivProductImage;
        TextView tvProductTitle;
        TextView tvProductPrice;
        TextView tvProductNum;

        public TitleViewHolder(View itemView) {
            super(itemView);
            ivProductImage = itemView.findViewById(R.id.iv_product_image);
            tvProductTitle = itemView.findViewById(R.id.tv_product_title);
            tvProductPrice = itemView.findViewById(R.id.tv_product_price);
            tvProductNum = itemView.findViewById(R.id.tv_product_num);

        }
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tvOrderNum;
        TextView tvTotalNum;
        TextView tvTotalPrice;
        TextView tv1;
        TextView tv2;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvOrderNum = itemView.findViewById(R.id.tv_order_num);
            tvTotalNum = itemView.findViewById(R.id.tv_total_num);
            tvTotalPrice = itemView.findViewById(R.id.tv_total_price);
            tv1 = itemView.findViewById(R.id.tv_2);
            tv2 = itemView.findViewById(R.id.tv_1);
        }
    }
}
